package com.mask.im.server.netty.protocol;

import com.alibaba.fastjson.JSON;
import com.mask.im.server.netty.model.IMReceiveMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * 协议解码器：解析[4字节长度+JSON消息体]格式的二进制帧，转换为业务对象
 *
 * @author jian
 */
@Log4j2
public class MessageProtocolDecoder extends MessageToMessageDecoder<TextWebSocketFrame> {
    // 类加载时打印日志，确认编码器被正确加载
    static {
        log.info("MessageProtocolEncoder 已加载");
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame, List<Object> list) throws Exception {
        IMReceiveMessage message = JSON.parseObject(textWebSocketFrame.text(), IMReceiveMessage.class);
        list.add(message);
    }
}